$(function() {
	$("#txtEmail").blur(function() {
		checkEmail(true);
	});

	$("#txtNickName").blur(function() {
		checkNickName();
	});

	$("#txtPassword").blur(function() {
		checkPassword();
	});
	$("#txtRepeatPass").blur(function() {
		checkRePassword();
	});

	$("#changeImage").click(function() {
		//修改src属性
			$("#imgVcode").attr("src",
					"../user/imageCode.action?dt=" + new Date().getTime());
			return false;
		});

	$("#txtVerifyCode").blur(function() {
		checkImageCode(true);
	});

	$("#registerForm").submit(
			function() {

				var ok = checkEmail(false) & checkNickName() & checkPassword()
						& checkRePassword() & checkImageCode(false);
				return ok == "0" ? false : true;
			});

	var email_falg = false;
	function checkEmail(async) {
		email_falg = false;
		var email = $("#txtEmail").val();
		$("#email\\.info").html("");
		//邮箱不能为空
		if (email == "") {
			$("#email\\.info").html("email不能为空!");
			return email_falg = false;
		}
		//邮箱格式校验
		var pattern = /\b(^['_A-Za-z0-9-]+(\.['_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))$)\b/;

		if (!pattern.test(email)) {
			$("#email\\.info").html("email格式错误!");
			return email_falg = false;
		}
		//邮箱是否存在
		$.ajax( {
			"url" : "checkEmail",
			"type" : "post",
			"data" : {
				"email" : email
			},
			"dataType" : "json",
			"success" : function(ok, status) {
				email_falg = ok;
				if (email_falg) {
					$("#email\\.info").html("email 可以使用");
					$("#email\\.info").attr("style", "color:green");
				} else {
					$("#email\\.info").html("email 已存在");
					$("#email\\.info").attr("style", "color:red");
				}
			},
			"async" : async
		});

		return email_falg;
	}
	function checkNickName() {

		var name = $("#txtNickName").val();

		$("#name\\.info").html("");

		if (name == "") {
			$("#name\\.info").html("昵称不能为空 ");
			return false;
		}

		var patten = /^[\w\u4e00-\u9fa5]{4,20}$/;

		if (!patten.test(name)) {
			$("#name\\.info").html("昵称格式错误 ");
			return false;
		} else {
			$("#name\\.info").html("");
		}
		return true;
	}
	function checkPassword() {
		var password = $("#txtPassword").val();
		var password1 = $("#txtRepeatPass").val();

		$("#password\\.info").html("");
		$("#password1\\.info").html("");

		if (password == "") {
			$("#password\\.info").html("密码不能为空");
			return false;
		}

		var patten = /\b(^[a-zA-Z0-9]{6,20}$)\b/;

		if (!patten.test(password)) {
			$("#password\\.info").html("密码格式错误");
			return false;
		} else if (password != password1 && password1 != "") {
			$("#password\\.info").html("两次密码输入不一致");
			return false;
		}
		return true;
	}

	function checkRePassword() {
		var pwd_flag = checkPassword();
		var password = $("#txtPassword").val();
		var password1 = $("#txtRepeatPass").val();

		if (password != "" && password1 == "") {
			$("#password1\\.info").html("二次密码不能为空");
			return false;
		} else {
			$("#password1\\.info").html("");
		}
		return pwd_flag;
	}
	var code_falg = false;
	function checkImageCode(async) {
		code_falg = false;
		var code = $("#txtVerifyCode").val();
		$("#number\\.info").html("");
		if (code == "") {
			$("#number\\.info").html("验证码不能为空");
			return code_falg = false;
		}

		$.ajax( {
			"url" : "checkImageCode",
			"type" : "post",
			"data" : {
				"imageCode" : code
			},
			"dataType" : "json",
			"success" : function(ok, status) {
				code_falg = ok;
				if (!code_falg) {
					$("#number\\.info").html("验证码错误");
				}
			},
			"async" : async
		});
		return code_falg;
	}
});
